package es.deusto.eside.programacion4.mario2d.states;

import java.sql.SQLException;
import java.util.List;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import org.newdawn.slick.util.Log;

import es.deusto.eside.programacion4.mario2d.Mario2D;
import es.deusto.eside.programacion4.mario2d.Bd.domain.Jugador;

public class Ranking extends BasicGameState {

	Image background;
	List<Jugador> lJugador;
	String nick;
	public int id;
	/**
	 * Fuente
	 */
	private UnicodeFont fpsFont;
	/**
	 * Ruta del estilo
	 */
	private static final String fontPath = "resources/ttf/mario.ttf";

	public Ranking(int ordinal) {
		this.id = ordinal;
	}

	@Override
	public void init(GameContainer arg0, StateBasedGame arg1)
			throws SlickException {
		fpsFont = new UnicodeFont(fontPath, 40, true, false);
		fpsFont.addAsciiGlyphs();
		fpsFont.addGlyphs(400, 600);
		fpsFont.getEffects().add(new ColorEffect(java.awt.Color.WHITE));
		fpsFont.loadGlyphs();
		background = new Image("resources/Images/Ranking/ranking.png");
		background = background.getScaledCopy(Mario2D.WIDTH, Mario2D.HEIGHT);
		try {
			lJugador = Mario2D.gBD.listJugador();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void enter(GameContainer container, StateBasedGame stateBasedGame)
			throws SlickException {
		if (Mario2D.p.isMusic() == true) {
			Mario2D.pole.loop();
		}
		init(container, stateBasedGame);
	}

	@Override
	public void leave(GameContainer container, StateBasedGame stateBasedGame)
			throws SlickException {
		Mario2D.pole.stop();
	}

	@Override
	public void render(GameContainer gc, StateBasedGame sb, Graphics g)
			throws SlickException {
		org.newdawn.slick.Color black = new org.newdawn.slick.Color(0, 0, 0,
				200);
		org.newdawn.slick.Color white = new org.newdawn.slick.Color(255, 255,
				255, 255);
		// Rect r1 = new Rect(3, Mario2D.HEIGHT-71 , 5, 5);
		// Rect r2 = new Rect(Mario2D.WIDTH-71, 3 , 5, 5);

		Rectangle rect = new Rectangle(38, 38, Mario2D.WIDTH - 76,
				Mario2D.HEIGHT - 76);
		// g.setFont(theme);
		g.drawImage(background, 0, 0);
		g.setColor(white);
		g.fillRect(33, 33, 5, Mario2D.HEIGHT - 71);
		g.fillRect(Mario2D.WIDTH - 38, 38, 5, Mario2D.HEIGHT - 76);
		g.fillRect(33, 33, Mario2D.WIDTH - 66, 5);
		g.fillRect(33, Mario2D.HEIGHT - 38, Mario2D.WIDTH - 66, 5);
		g.setColor(black);
		g.fill(rect);
		Image title = new Image("resources/Images/Option/title.png");
		g.drawImage(title, Mario2D.WIDTH / 2 - title.getWidth() / 2, 0);
		// g.drawString(Locale.INSTANCE.getText("Option"), 280, 30);
		fpsFont.drawString(240, 20, "Puntos");
		int distanciaDibujo = 130;
		for (int i = 0; i < lJugador.size(); i++) {
			Jugador j = lJugador.get(i);
			fpsFont.drawString(70, 130 + i * 50, j.toString());
		}

	}

	@Override
	public void update(GameContainer gc, StateBasedGame sb, int delta)
			throws SlickException {
		Input i = gc.getInput();
		if (i.isKeyPressed(Input.KEY_ENTER)) {
			init(gc, sb);
			sb.enterState(GameState.MENU_STATE.ordinal(),
					new FadeOutTransition(), new FadeInTransition());
		}
	}

	@Override
	public int getID() {
		return this.id;
	}

}
